//
//  LGQuickReplyView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/24.
//  快捷回复

#import "LGQuickReplyView.h"

@interface LGQuickReplyView()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGEmptyView *emptyView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)BOOL isfirst;

@end

@implementation LGQuickReplyView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        [self addSubview:self.addButton];
        [self addSubview:self.tableView];
        [self addSubview:self.emptyView];
        [self performSelector:@selector(startBufferAction) withObject:nil afterDelay:0.5];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"editQuickReplyMessage" object:nil];
        [self requestData];
    }
    return self;
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/im/quickMessageList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView3" content:@"暂无快捷回复" offset:45];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView3" content:@"暂无快捷回复" offset:45];
        }
    }];
}

//添加快捷回复
-(void)addQuickReplyButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(addQuickReplyAction)]){
        [self.delegate addQuickReplyAction];
    }
}

-(void)startBufferAction{
    if (self.isfirst == YES) {
        [self.emptyView startBufferAction];
        self.isfirst = NO;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGQuickReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.content = self.dataArray[indexPath.row][@"content"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    if(self.delegate && [self.delegate respondsToSelector:@selector(quickReplyViewDidSendText:)]){
        [self.delegate quickReplyViewDidSendText:LGNSString(tempDic[@"content"])];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat height = self.bounds.size.height;
    self.tableView.frame = CGRectMake(0, viewPix(45), Screen_W, height-viewPix(45));
    self.emptyView.frame = CGRectMake(0, viewPix(45), Screen_W, height-viewPix(45));
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"添加/编辑快捷回复" titleFont:LGFont(16) textColor:@"#3291FF" imageName:@"快捷回复-添加"];
        _addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _addButton.frame = CGRectMake(viewPix(20), 0, Screen_W/2.0, viewPix(45));
        _addButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(8), 0, 0);
        [_addButton addTarget:self action:@selector(addQuickReplyButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView registerClass:[LGQuickReplyCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(20))];
    }
    return _tableView;
}

-(LGEmptyView *)emptyView{
    if(!_emptyView){
        _emptyView = [[LGEmptyView alloc] init];
        _emptyView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        _emptyView.margin = -10;
        _emptyView.delegate = self;
        _emptyView.needBuffer = YES;
    }
    return _emptyView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


@interface LGQuickReplyCell()

/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGQuickReplyCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.lineView];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(20));
            make.right.equalTo(self.contentView).offset(-viewPix(15));
            make.top.equalTo(self.contentView).offset(viewPix(12));
            make.bottom.equalTo(self.contentView).offset(-viewPix(12));
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.contentView);
            make.height.equalTo(@(0.5));
        }];
    }
    return  self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
    [self.contentLabel lineSpacing:3];
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E2E2E2"];
    }
    return _lineView;
}

@end
